package org.cwnuit.course.action;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.course.domain.Account;
import org.cwnuit.course.domain.Classgroup;
import org.cwnuit.course.domain.Coursebox;
import org.cwnuit.course.service.AccountService;
import org.cwnuit.course.service.ClassgroupService;
import org.cwnuit.course.service.CourseboxService;
import org.cwnuit.course.util.ReflectionUtils;
import org.cwnuit.course.service.CourseService;
import org.cwnuit.course.domain.Course;
/**
 * 用户管理Controller
 * @author john
 */
@SuppressWarnings("serial")
public class AccountController extends HttpServlet {
	
	AccountService accountService = new AccountService();
	CourseService courseService = new CourseService();
	CourseboxService courseboxService = new CourseboxService();
	ClassgroupService classgroupService = new ClassgroupService();
	/**
	 * 用户登陆
	 */
	public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Account account = accountService.loginAccount(username);
		if (username != "" && password != "") {
			if (account == null  ) {
				linkMsg("","box-error", "对不起登陆失败，用户名错误！" , "login.jsp" , request, response);
			} else if(!account.getPassword().equals(password)){
				linkMsg("","box-error", "对不起登陆失败，密码错误！" , "login.jsp" , request, response);
			} else {
				request.getSession().setAttribute("account", account);
				Coursebox coursebox = courseboxService.getMAXCoursebox();
				request.getSession().setAttribute("coursebox", coursebox);
				linkMsg("","box-info", "恭喜你登陆成功" , "web/base/main.jsp" , request, response);
			}
		} else {
			linkMsg("","box-error", "请认真填写，用户名和密码不能为空！" , "login.jsp" , request, response);
		}
	}
	
	/**
	 * 用户注销
	 */
	public void logout(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("account");
		request.getSession().removeAttribute("coursebox");
		linkMsg("","box-info", "您已经成功注销！" , "login.jsp" , request, response);
	}
	
	/**
	 * 进入用户注册界面
	 */
	public void register(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		List<Course> listCourse = courseService.listCourse();
		List<Classgroup> listClassgroup = classgroupService.listClassgroup();
		request.setAttribute("listCourse", listCourse);
		request.setAttribute("listClassgroup", listClassgroup);
		linkMsgRequest("","box-info", "请认真填写个人信息！" , "web/users/register.jsp" , request, response);
	}
	
	/**
	 * 用户注册的提交
	 */
	public void registersubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		if(accountService.saveAccount(request)){
			linkMsg("","box-info", "你已经成功注册！" , "login.jsp" , request, response);
		}else{
			linkMsg("","box-error", "对不起未注册成功！" , "web/users/register.jsp" , request, response);
		}
	}
	
	/**
	 * 管理员添加新用户和修改的提交
	 */
	public void usersubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String useridStr = request.getParameter("userid")==""?"0":request.getParameter("userid");
		int userid = Integer.parseInt(useridStr);
		if(accountService.saveAccount(request)){
			if (userid == 0) {
				List<Course> listCourse = courseService.listCourse();
				List<Classgroup> listClassgroup = classgroupService.listClassgroup();
				request.setAttribute("listCourse", listCourse);
				request.setAttribute("listClassgroup", listClassgroup);
				linkMsg("添加新用户","box-info", "恭喜你，新用户已经添加成功！" , "web/users/userinput.jsp" , request, response);
			} else {
				List<Account> listAccount = accountService.listAccount();
				request.setAttribute("listAccount", listAccount);
				linkMsg("用户列表","box-info", "恭喜你，成功修改用户信息！" , "web/users/userslist.jsp" , request, response);
			}
		}else{
			List<Account> listAccount = accountService.listAccount();
			request.setAttribute("listAccount", listAccount);
			linkMsg("用户列表","box-error", "对不起你的操作未能成功！" , "web/users/userslist.jsp" , request, response);
		}
	}
	
	/**
	 * 进入管理员查看用户列表
	 */
	public void userlist(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		List<Account> listAccount = accountService.listAccount();
		request.setAttribute("listAccount", listAccount);
		linkMsgRequest("用户列表","box-info", "你可以对用户进行添加、修改、删除等操作！" , "web/users/userslist.jsp" , request, response);
	}
	
	/**
	 * 进入管理员添加新用户和修改的界面
	 */
	public void userinput(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int userid = Integer.parseInt(request.getParameter("userid"));
		List<Course> listCourse = courseService.listCourse();
		List<Classgroup> listClassgroup = classgroupService.listClassgroup();
		request.setAttribute("listCourse", listCourse);
		request.setAttribute("listClassgroup", listClassgroup);
		if (userid == 0) {
			linkMsgRequest("添加新用户","box-info", "请认真填写用户信息！" , "web/users/userinput.jsp" , request, response);
		} else {
			Account account = accountService.getAccount(userid);
			request.setAttribute("user", account);
			linkMsgRequest("修改用户信息","box-info", "请认真填写用户信息！" , "web/users/userinput.jsp" , request, response);
		}
	}
	
	/**
	 * 进入用户修改个人信息界面
	 */
	public void userset(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		List<Course> listCourse = courseService.listCourse();
		List<Classgroup> listClassgroup = classgroupService.listClassgroup();
		request.setAttribute("listCourse", listCourse);
		request.setAttribute("listClassgroup", listClassgroup);
		linkMsgRequest("修改个人信息","box-info", "请认真填写个人信息！" , "web/users/userset.jsp" , request, response);
	}
	/**
	 * 用户修改个人信息提交
	 */
	public void usersetsubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		if(accountService.saveUserset(request)){
			request.getSession().removeAttribute("account");
			int userid = Integer.parseInt(request.getParameter("userid"));
			Account account = accountService.getAccount(userid);
			request.getSession().setAttribute("account", account);
			linkMsg("修改个人信息","box-info", "恭喜你！你已经成功修改个人信息。" , "web/users/userset.jsp" , request, response);
		}else{
			linkMsg("修改个人信息","box-error", "对不起！未能成功修改个人信息！" , "web/users/userset.jsp" , request, response);
		}
	}
	/**
	 * 删除用户信息
	 */
	public void deleteAccount(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("userid")==null?"0":request.getParameter("userid");
		int userid = Integer.parseInt(uid);
		
		boolean deleteBoolean = accountService.deleteAccount(userid);
		List<Account> listAccount = accountService.listAccount();
		request.setAttribute("listAccount", listAccount);
		
		if (deleteBoolean) {
			linkMsgRequest("用户列表","box-info", "恭喜你，你已经成功删除一条用户信息！" , "web/users/userslist.jsp" , request, response);
		} else {
			linkMsgRequest("用户列表","box-error", "对不起，删除用户信息失败！" , "web/users/userslist.jsp" , request, response);
		}
	}
	/**
	 * 进入用户修改个人信息界面
	 */
	public void usersetpw(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		linkMsg("修改个人密码","box-info", "请认真填写新旧密码信息！" , "web/users/usersetpw.jsp" , request, response);
	}
	/**
	 **跳转与提示信息 公共方法
	 **信息级别的描述-msgrank
	 *级别0-普通框 值空
	 *级别1-普通信息框 值box-info
	 *级别2-普通警告框 值box-warning
	 *级别3-普通错误框 值box-error
	 *级别4-错误信息列表框 值box-error-msg
	 *参数：标题 信息的样式 提示信息 跳转地址
	 */
	public void linkMsgRequest(String tittle, String msgrank, String msg, String linkStr, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.getSession().setAttribute("content_tittle",tittle);
		request.getSession().setAttribute("msgrank",msgrank);
		request.getSession().setAttribute("msg",msg);
		request.getRequestDispatcher(linkStr).forward(request,response);
	}
	//直接跳转
	public void linkMsg(String tittle, String msgrank, String msg, String linkStr, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.getSession().setAttribute("content_tittle",tittle);
		request.getSession().setAttribute("msgrank",msgrank);
		request.getSession().setAttribute("msg",msg);
		response.sendRedirect(linkStr);
	}
	
	/**
	 * 用户数据Controller过滤器
	 * cmd即为servlet中的方法名
	 * 公共方法，勿动！
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		Method method = ReflectionUtils.getDeclaredMethod(AccountController.class, cmd);
		try {
			if(method!=null){
				method.invoke(new AccountController(),request, response);
			}else{
				request.getRequestDispatcher("index.jsp").forward(request,response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
